
#include "front/front_interface.h"

using namespace std;
using namespace ppspeech;


bool FrontEngineInterface::FrontContext_AddChineseContext(const string& fpInKey, const string& fpInValue, WriteDictDatabase& fpOutWrBiasDictDb, ofstream& fpOutJiebaUser)
{
	vector<string> tSplit = absl::StrSplit(fpInValue, " ");

	bool tbLegalFlag = true;
	string temp;
	string tPhoneme = "";
	for(string p: tSplit) {
		// 判断key是否在拼音库里，如果不在则认为是无效拼音
		if ( "" != (temp = mDictionary.find_sqlite_dict(DatabaseDictTableZhSymbol, p)) )
		{
			tPhoneme += temp + " ";
		}
		else {
			if(p.length() >= 1 && p[p.length()-1] == 'r') {	//儿化音
				if(p!="ēr" && p!="ér" && p!="ěr" && p!="èr" && p!="er") {
					p = p.substr(0, p.length()-1);			//去掉r

					// 判断key是否在拼音库里，如果不在则认为是无效拼音
					if ( "" != (temp = mDictionary.find_sqlite_dict(DatabaseDictTableZhSymbol, p)) )
					{
						tPhoneme += temp + " er5 ";
						continue;
					}
				}
			}

			tbLegalFlag = false;
			return false;		//非法拼音
		}
	}

	if(tbLegalFlag)
	{
		tPhoneme = tPhoneme.substr(0, tPhoneme.length()-1);	//去掉尾部的空白

		if(tSplit.size() > 1) {		//词组
			if( fpOutJiebaUser.good() ) {
				if( false == fpOutWrBiasDictDb.add_sqlite_dict(DatabaseDictTableZhPhrase, true, fpInKey, tPhoneme) ) {
					return false;
				}

				const string tLineJieba = fpInKey + " 10000 nz" + "\n";
				fpOutJiebaUser.write(tLineJieba.c_str(), tLineJieba.length());
			}
		}
		else {	//单字
			if( false == fpOutWrBiasDictDb.add_sqlite_dict(DatabaseDictTableZhChar, true, fpInKey, tPhoneme) ) {
				return false;
			}
		}
	}

	return true;
}

bool FrontEngineInterface::FrontContext_AddEnglishContext(const string& fpInKey, const string& fpInValue, WriteDictDatabase& fpOutWrBiasDictDb)
{
	string tInValue = fpInValue;
	StrReplace(tInValue, "ː", ":");

	vector<string> tSymbols = SplitUtf8(tInValue);
	vector<string> tCombine;
	bool tSkipFlag = false;
	size_t tSymbolsSize = tSymbols.size();
	const string tIgnoreStr = "[]/ˈˌ ()";
	
	//修正合并
	for(size_t i=0; i<tSymbolsSize; i++) {
		string tCurr = tSymbols[i];

		if(tCurr != "ʌ" && tIgnoreStr.find_first_of(tCurr) != string::npos) {
			continue;
		}

		if(tSkipFlag) {
			tSkipFlag = false;
			continue;
		}

		if(i+1 < tSymbolsSize) {
			const string tNext = tSymbols[i+1];
			if( (tNext==":")
				||  ( (tCurr=="d" && tNext=="ʒ") || (tCurr=="t" and tNext=="ʃ") )
				||  ( tCurr=="a" &&  (tNext=="ɪ" || tNext=="ʊ") )
				||  ( tCurr=="e" &&  (tNext=="ɪ" || tNext=="ə") )
				||  (tCurr=="ɔ" && tNext=="ɪ")
				||  (tCurr=="ɪ" && tNext=="ə")
				||  (tCurr=="o" && tNext=="ʊ")
				||  (tCurr=="ə" && tNext=="ʊ")
				||  (tCurr=="ʊ" && tNext=="ə")
			) {
				tSkipFlag = true;
				tCombine.push_back(tCurr + tNext);
				continue;
			}
			tCombine.push_back(tCurr);
		}
	}

	string temp;
	string tPhoneme = "";
	for(string c : tCombine) {
		if ( "" != (temp = mDictionary.find_sqlite_dict(DatabaseDictTableEnSymbol, c)) )
		{
			tPhoneme += temp + " ";
		}
		else {
			cout << c << endl;
			return false;
		}
	}
	if(tPhoneme.length() > 0) {
		tPhoneme = tPhoneme.substr(0, tPhoneme.length()-1);	//去掉最后的空格
	}

	if( false == fpOutWrBiasDictDb.add_sqlite_dict(DatabaseDictTableEnWord, true, fpInKey, tPhoneme) ) {
		return false;
	}

	return true;
}


//读取热词文件，更新热词字典。
void FrontEngineInterface::FrontUpdateBiasFile(const string fInContextFilepath, const string fInDictionaryFolderpath)
{
	ifstream tInContext(fInContextFilepath.c_str(), ifstream::in);
	ofstream tOutJiebaUser((fInDictionaryFolderpath + "/jieba/user.dict.utf8").c_str(), ifstream::out);
	WriteDictDatabase tWrDictDbBias(mDbBiasDictPath);

	vector<string> tVecIllegalLine;

	if( tInContext.good() && tOutJiebaUser.good() ) {
		string tContextLine;
		while (getline(tInContext, tContextLine)) {
			vector<string> tSplit = StrSplit(tContextLine, "\t");
			if(tSplit.size() != 3) {
				continue;
			}
			string tStrLanguage = tSplit[0];
			string tStrKey = tSplit[1];
			string tStrValue = tSplit[2];

			StrReplace(tStrValue, "ɡ", "g");
			StrReplace(tStrValue, "'", "");

			if( tStrLanguage == "zh" or tStrLanguage == "cn" ) {	//中文
				if(false == FrontContext_AddChineseContext(tStrKey, tStrValue, tWrDictDbBias, tOutJiebaUser) ) {
					tVecIllegalLine.push_back(tContextLine);
				}
			}
			else if( tStrLanguage == "en" ) {						//英文
				if(false == FrontContext_AddEnglishContext(tStrKey, tStrValue, tWrDictDbBias) ) {
					tVecIllegalLine.push_back(tContextLine);
				}
			}
		}
	}

	tInContext.close();
	tOutJiebaUser.close();

	cout << "========== 用户热词非法音标 ============" << endl;
	for(string tIllegalLine: tVecIllegalLine) {
		cout << tIllegalLine << endl;
	}
	cout << "========== ============= ============" << endl;
}
